/*
 * CallDetailRecords.java
 *
 * Created on July 24, 2006, 8:52 AM
 *
 * @author Francois Daniels
 */

package za.gateway.backend;

import za.gateway.container.GraphData;
import za.gateway.container.CallDetailRecord;
import za.gateway.io.DatabaseInterface;
import za.gateway.io.Log;
import java.sql.ResultSet;
import java.util.Vector;
import java.sql.Timestamp;

/**
 * This class will contain the list of call detail records. It will be 
 * responsible for retrieving this data from the database and 
 * transforming into a suitable form. It will be used by the 
 * configuration tool to extract various subsets of relevant data.
 *
 * @author Francois Daniels
 */
public class CallDetailRecords {
    /** Variables belonging to the CallDetailRecords class */
    
    /** 
     * This is an instance of the DatabaseInterface class. It will be 
     * used by the CallDetailRecords class to query and retrieve data 
     * from the database.
     **/
    protected DatabaseInterface Database;
    
    /** This is an vector of all the CDRs stored by the system. */
    protected Vector Data;
    
    /** 
     * This is one of the AxisTypes used to determine which data range 
     * the user can choose from for a particular axis.
     */
    public final static int HOUR   = 0;
    
    /** 
     * This is one of the AxisTypes used to determine which data range 
     * the user can choose from for a particular axis.
     */
    public final static int DAY    = 1;
    
    /** 
     * This is one of the AxisTypes used to determine which data range 
     * the user can choose from for a particular axis.
     */
    public final static int GROUP  = 2;
    
    /** 
     * This is one of the AxisTypes used to determine which data range 
     * the user can choose from for a particular axis.
     */
    public final static int NUMBER_OF_CALLS  = 3;
    
    /** 
     * This is one of the AxisTypes used to determine which data range 
     * the user can choose from for a particular axis.
     */
    public final static int CALL_DURATION    = 4;
    
    
    /** Functions belonging to the CallDetailRecords class */
    
    /**
     * This function will query the database for the call detail 
     * records. They will be stored in the array, Data.
     */
    protected void ReadDB () {
        
        // Retrieve the data from the database
        ResultSet results = Database.Query ("Select * from CDR");        
        if (results == null) return;
        
        try {
            while (results.next ()) {
                
                // Add the data to our list
                Data.add (new CallDetailRecord (
                        results.getObject("accountcode").toString (),
                        results.getObject("src").toString (),
                        results.getObject("dst").toString (),
                        new Integer (
                            results.getObject("duration").toString ()
                            ).intValue (),
                        (Timestamp.valueOf (
                            results.getObject("calldate").toString ()
                            )).getTime ()));
            } 
        }catch (java.sql.SQLException Ex) {
            Log.ERROR(Ex);
        }
        
        try {
            results.close ();
        } catch (Exception e) {}
    }
    
    /**
     * This function will call ReadDB ( ) to get the latest list of 
     * call detail records. It will then return the list of call detail 
     * records stored in this class.
     *
     * @return The array of call detail records stored in the system.
     */
    public CallDetailRecord [] GetData () {
        return (CallDetailRecord []) Data.toArray (new CallDetailRecord 
                [Data.size ()]);
    }
    
    /**
     * This function will return the graphData associated with the 
     * arguments 
     * specified. The GraphData class contains two arrays which 
     * correspond to the domain and the range of the data.
     * 
     * @param Domain        Is one of [HOUR, DAY, GROUP, 
     *                      NUMBER_OF_CALLS, CALL_DURATION].
     * @param Range         Is one of [HOUR, DAY, GROUP, 
     *                      NUMBER_OF_CALLS, CALL_DURATION].
     * @return              null if there are no call detail records. 
     *                      Else returns the requested data.
     */
    public GraphData GetGraphData (int Domain, int Range) {
        //TODO: Calculate the graph data then return it
        
        //TODO: return the actual data.
        return null;
    }
    
    /** 
     * Creates a new instance of CallDetailRecords.
     *
     * @param Username      The username for the SQL database.
     * @param Password      The password for the SQL database.
     * @param HostName      The hostname on which the SQL server is 
     *                      located.
     * @param DatabaseName  The name of the database schema the data 
     *                      is in.
     */
    public CallDetailRecords (String Username, String Password, 
            String HostName, String DatabaseName) {
        
        Database = new DatabaseInterface (Username, Password, HostName, 
                                          DatabaseName);
        Data = new Vector ();
        ReadDB (); //Read the data into the class from the database
    }    
}
